package tiltmaze.loader;

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import tiltmaze.Maze;

/**
 * Class used for loading a maze from a file.
 * @author Juraj Nižnan
 */
public class MazeLoader {

    /**
     * @param fileName name of the file with the maze
     * @return maze from file or null if the file does not have the proper content
     */
    public static Maze importMaze(String fileName) {
        int[][] arr;
        try {
            Scanner scanner = new Scanner(new FileReader(new File(fileName)));
            String line = scanner.nextLine();
            String[] split = line.split(",");
            int r = Integer.parseInt(split[0]);
            int c = Integer.parseInt(split[1]);
            arr = new int[r][c];
            for (int i = 0; i < r; i++) {
                line = scanner.nextLine();
                split = line.split(",");
                int[] row = new int[c];
                for (int j = 0; j < c; j++) {
                    row[j] = Integer.parseInt(split[j]);
                }
                arr[i] = row;
            }
        } catch (Exception exception) {
            return null;
        }
        return new Maze(arr);
    }
}
